﻿#define _CRT_SECURE_NO_WARNINGS     //Ignore the warning of Visual Studio
#define MAXLENGTH 32
#define PATH_MAXLENGTH 260
#include "include/base.h"
#include "include/plugins.h"
#include "include/shortcut.h"
#include "include/Version.h"

struct App ShortCut[512]; //store the ShortCuts
struct Plan Plans[63];      //store the plans
int appcount;
uint8 plancount;
uint8 FillCount,ICount;                //to save the number of the char that autofix and the char inputted

int ResultIndex;            //to save the result Index of the string matching if matching successfully
bool InitFinish;
bool IsDetected;                //to save the result of the string matching
char InputChar[MAXLENGTH];          //to save the char  inputted
char RunPath[PATH_MAXLENGTH];
char Plugins[32];
extern bool ShouldSports,ShouldDrink,FirstSports,FirstDrink;
extern time_t nowtime;
extern struct tm * timeinfo;
extern struct time *CheckSports,*CheckDrink,*starttime;

void Init();                        //Initilize
void Clear();
void Exit();

int main(void)
{
    printf("**********************************************\n     Welcome to Amarillys's World V1.005\n**********************************************");
    printf("\n*****            Run :");PrintTime();printf("           *****");

	Init();
    GetTime(starttime);
    AddPlugins(Plugins);		                	        //load the plugin keys
	LoadShortCut(ShortCut, &appcount);	        //load shortcuts from app.dat
    LoadPlan(Plans);
    atexit(Exit);

    KBDETECT:
        while(true){                          //the main loop
            if(!InitFinish)
                Clear();			                //Initialize
            if(kbhit()){                      //detect the keyboard
                InitFinish = false;
                goto CMDSTART;               //start
            }else{
                Sleep(50);                     //slow the loop
                CheckPlan(Plans);        //detect the plan
            }
        }

    CMDSTART:
	    while((InputChar[ICount] = _getch())!= '\r')				//If the input char is 'Enter' or it has enter the function of plugins,A loop finish.
        {
            if (InputChar[0] == '\b' || DetectPlugins(InputChar[0], Plugins)){			//deal with the backspace,regard it as a plugins key and Detect  plugins and run if is.
				goto KBDETECT;
			}else if(InputChar[0] == '#'){               //Add shortcut function
				AddShortCut(ShortCut, &appcount);
				goto KBDETECT;
            }else if(InputChar[0] == '+'){
                AddPlan();
				goto KBDETECT;
			}else if(InputChar[0] == 0xe0 )   //deal with the leftward key
                if(_getch() == 72)
                    goto KBDETECT;

            if (InputChar[ICount] == '\b'){			//deal with the backspace,but it cannot be regard as a plugin when it isn't the first input
                AntiFill(FillCount);			            //del the filled char
                DelCurrentChar();
                InputChar[ICount--] = 0;                   //Icount--
            }else if(InputChar[ICount] == -32 ){   //deal with the leftward key
                if(  _getch()  == 75){
                    putchar('\b');
                    ICount--;
                }
            }else{
                putchar(InputChar[ICount++]);		//ICount++,Display the inputchar
                AntiFill(FillCount);			            //del the filled char
                if ((IsDetected = DetectChar(InputChar, &ResultIndex)) == true)			//Detect the inputstring and get the result.
                    FillCount = AutoFillBack(strlen(InputChar), ShortCut[ResultIndex].Name,true);
                else
                    AntiFill(FillCount);			        //del the filled char
            }
            //safe checking
            if(ICount >= MAXLENGTH){
                printf("\n Error : Over Input!");
                PrintTime();
                goto KBDETECT;
            }
        }

        if(IsDetected){
            strcat(RunPath, "start ");			        //Merge the string to be ready to run
            strcat(RunPath, ShortCut[ResultIndex].Path);
            AutoFillBack(strlen(InputChar)-1,ShortCut[ResultIndex].Name,false);
            printf("    ");
            system(RunPath);
        }else{
            strcat(RunPath, "start ");
            strcat(RunPath,InputChar);
            AutoFillBack(strlen(InputChar)-1,ShortCut[ResultIndex].Name,false);
            printf("    ");
            system(RunPath);
        }
        printf("    ||---> ");
        PrintTime();
        goto KBDETECT;
    return 0;
}

void Clear(){      //custom function - Initilization
	memset(InputChar, 0, MAXLENGTH);
	memset(RunPath, 0, PATH_MAXLENGTH);
	FillCount = ICount = 0;
	IsDetected = false;
	printf("\n\n Maikaze Seikai ----> ");
	InitFinish = true;
}

void Init(){
	ShouldDrink = ShouldSports = FirstSports = FirstDrink = true;
    starttime = (struct time * )malloc(sizeof(struct time));
    CheckDrink = (struct time * )malloc(sizeof(struct time));
    CheckSports = (struct time * )malloc(sizeof(struct time));
}

void Exit(){
    free(starttime);
    free(CheckDrink);
    free(CheckSports);
    for(int i = 0;i < plancount;i++)
        free(Plans[i].ptime);
}
